package engine.asset
{
	import engine.core.*;
	import engine.graphics.vfx.*;
	import engine.loaders.*;
	
	public class RxAssetVFX extends RxAsset
	{
		private var m_assetGroup:RxAssetGroup;
		private var m_chains:Object;
		private static var s_type:int;
		private static const STATE_XMLREADY:int = 4;
		private static const STATE_LOADINGASSETS:int = 5;
		
		public function RxAssetVFX()
		{
			super(s_type);
		}
		
		public static function Initialize() : void
		{
			s_type = RxAsset.RegisterType("vfx", CreateFromXML);
		}
		
		public function Init(param1:XML) : void
		{
			var _loc_2:XML = null;
			var _loc_3:String = null;
			var _loc_4:RxWorldFXChain = null;
			this.m_assetGroup = new RxAssetGroup();
			this.m_assetGroup.SetUsedByGPU(true);
			RxWorldFXManager.instance.ParseFromXML(param1, this.m_assetGroup);
			this.m_chains = new Object();
			for each (_loc_2 in param1.chain)
			{
				
				_loc_3 = _loc_2.@name;
				_loc_4 = new RxWorldFXChain();
				_loc_4.ParseFromXML(_loc_2);
				this.m_chains[_loc_3] = _loc_4;
			}
			m_state = STATE_XMLREADY;
		}
		
		public static function CreateFromXML(param1:XML, param2:String) : RxAssetVFX
		{
			var _loc_3:* = param1.@name;
			var _loc_4:* = param1.@url;
			_loc_4 = RxUtilities.MergePath(param2, _loc_4);
			return Create(_loc_4, _loc_3);
		}
		
		public static function Create(param1:String, param2:String) : RxAssetVFX
		{
			var _loc_3:* = GetByURL(param1);
			if (_loc_3 != null)
			{
				var _loc_4:* = _loc_3;
				var _loc_5:* = _loc_3.m_references + 1;
				_loc_4.m_references = _loc_5;
				return _loc_3;
			}
			_loc_3 = new RxAssetVFX;
			_loc_3.SetURL(param1);
			_loc_3.SetName(param2);
			return _loc_3;
		}
		
		public static function GetByURL(param1:String) : RxAssetVFX
		{
			return RxAssetVFX(_getByURL(param1, s_type));
		}
		
		public static function GetByName(param1:String) : RxAssetVFX
		{
			return RxAssetVFX(_getByName(param1, s_type));
		}
		
		public function SpawnChain(param1:String, param2:RxWorldFXManager, param3:RxLoader3D) : void
		{
			var _loc_4:RxWorldFXChain = null;
			if (param1)
			{
				_loc_4 = this.m_chains[param1];
				if (_loc_4)
				{
					_loc_4.Spawn(param2, param3, 1);
				}
			}
			else
			{
				for each (_loc_4 in this.m_chains)
				{
					
					_loc_4.Spawn(param2, param3, 1);
				}
			}
		}
		
		override public function Load() : void
		{
			super.Load();
			switch(m_state)
			{
				case STATE_EMPTY:
				{
					m_loader = StartLoad(m_url, RxLoader.TYPE_TEXT, this.OnXMLLoaded);
					m_state = STATE_LOADING;
					break;
				}
				case STATE_LOADING:
				{
					break;
				}
				case STATE_XMLREADY:
				{
					this.m_assetGroup.AddCallback(new RxAssetCallback(this.OnAssetsLoaded));
					this.m_assetGroup.Load();
					m_state = STATE_LOADINGASSETS;
					break;
				}
				case STATE_LOADINGASSETS:
				{
					break;
				}
				case STATE_ERROR:
				{
					ExecuteCallbacks(false);
					break;
				}
				case STATE_READY:
				{
					ExecuteCallbacks(true);
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		private function OnXMLLoaded(param1:RxLoaderCallback) : void
		{
			var _loc_2:XML = null;
			if (param1.m_result == true)
			{
				m_size = param1.m_loader.GetSizeInBytes();
				_loc_2 = XML(param1.m_loader.GetText());
				this.Init(_loc_2);
				this.Load();
			}
			else
			{
				m_state = STATE_ERROR;
				ExecuteCallbacks(false);
			}
		}
		
		private function OnAssetsLoaded(param1:RxAssetCallback) : void
		{
			if (param1.m_result == true)
			{
				m_state = STATE_READY;
				ExecuteCallbacks(true);
			}
			else
			{
				m_state = STATE_ERROR;
				ExecuteCallbacks(false);
			}
		}
	}
}